package org.iteam.web.struts.actionmapper;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;

import com.opensymphony.xwork2.config.ConfigurationManager;

public class RestfulActionMapper extends DefaultActionMapper {
//	protected static final Logger LOG = LoggerFactory
//			.getLogger(RestfulActionMapper.class); 

	public ActionMapping getMapping(HttpServletRequest request,
			ConfigurationManager configManager) {
		// System.out.println("METHOD:" + request.getMethod());
		// String uri = RequestUtils.getServletPath(request);
		// System.out.println("URL:" + uri);
		// String fullName = uri.substring(1);
		// int actionNameIndex = uri.indexOf('/', 1);
		// String actionName = "";
		// String nameSpace = "/" + "";
		// String methodName = "";
		// Map<String, Object> params = new HashMap<String, Object>();
		// return new ActionMapping(actionName, nameSpace, methodName, params);
		ActionMapping mapping = new ActionMapping();
		String uri = getUri(request);

		int indexOfSemicolon = uri.indexOf(";");
		uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon)
				: uri;

		uri = dropExtension(uri, mapping);
		if (uri == null) {
			return null;
		}

		
		parseNameAndNamespace(uri, mapping, configManager);

		handleSpecialParameters(request, mapping);

		if (mapping.getName() == null) {
			return null;
		}

		parseActionName(mapping);

		return mapping;
	}
}
